var app = getApp();
var c = require('../common.js');

Page({
  data: {
    showPayPwdInput: false, //是否展示密码输入层
    showPayPwdInputwo: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    pwdValtwo: '', //输入的密码
    payFocus: true, //文本框焦点
    payFocustwo: true, //文本框焦点
  },
  onLoad: function(option) {
    var goods_id = option.goods_id;
    var typeinfo = option.type ? option.type:0;
    var money = option.money;
    var tuan = option.tuan;
    var vip = option.vip;
    if (typeinfo==1){
      var id = option.id;
      var order_id = option.order_id;
      var order_num = option.order_num;
      this.setData({
        id: id,
        order_id: order_id,
        order_num: order_num
      })
    }

    this.setData({
      goods_id: goods_id,
      money: money,
      tuan: tuan,
      vip: vip,
      typeinfo: typeinfo
    })
    this.showInputLayer();
  },
  /**
   * 显示支付密码输入层
   */
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  /**
   * 显示支付密码输入层two
   */
  showInputLayertwo: function() {
    this.setData({
      showPayPwdInputtwo: true,
      payFocus: true
    });
  },
  /**
   * 隐藏支付密码输入层one
   */
  hidePayLayer: function() {
    var val = this.data.pwdVal; //密码
    var user_id = app.u.user_id;
    this.setData({
      showPayPwdInput: false,
      showPayPwdInputwo: true,
      payFocus: false,
      pwdVal: val
    });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayertwo: function() {
    var goods_id = this.data.goods_id;
    var money    = this.data.money;
    var tuan     = this.data.tuan;
    var vip      = this.data.vip;
    var typeinfo = this.data.typeinfo;
    var order_id = this.data.order_id;
    var valtwo = this.data.pwdValtwo; //密码二次
    var val = this.data.pwdVal; //密码
    var that = this;
    var user_id = app.u.user_id;
    if (valtwo != val) { //如果两次密码不同
      wx.showToast({
        title: '两次密码不同,请从新设置',
        icon: 'none',
      })
      setTimeout(function() {
        that.setData({
          showPayPwdInputwo: false,
          showPayPwdInput: true,
          payFocus: false,
          pwdVal: '',
          pwdValtwo: '',
        })
      }, 1000)
      return;
    } else { //如果两次密码相同
      that.setData({
        showPayPwdInputtwo: false,
        payFocustwo: false,
        pwdValtwo: ''
      }, function() {
        c.ajax(
          app.url + 'setpassword', {
            user_id: user_id,
            value: val
          },
          res => {
            if (res.data == 1) {
              wx.showToast({
                title: '设置成功',
                icon: 'none'
              })

              setTimeout(function() {
                 if(typeinfo==1){
                   var order_id = that.data.order_id;
                   var id = that.data.id;
                   var order_num = that.data.order_num;
                   wx.redirectTo({
                     url: '/pages/sendneedorder/sendneedorder?order_id='+order_id+'&id='+id+'&order_num='+order_num,
                   })
                   //在我的账户修改支付密码
                 } else if (typeinfo==2){
                   wx.redirectTo({
                     url: '/pages/myzhanghu/myzhanghu',
                   })
                 }else{
                   wx.redirectTo({
                     url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money + '&tuan=' + tuan + '&vip=' + vip,
                   })
                 }


                // wx.switchTab({
                //   url: '/pages/index/index',
                //   success: function(res) {},
                //   fail: function(res) {},
                //   complete: function(res) {},
                // })
              }, 2000)



            }
          }
        )
      });
    }
  },
  /**
   * 获取焦点
   */
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function(e) {
    this.setData({
      pwdVal: e.detail.value
    });
    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }
  },
  /**
   * 输入密码监听two
   */
  inputPwdtwo: function(e) {
    this.setData({
      pwdValtwo: e.detail.value
    });

    if (e.detail.value.length >= 6) {
      this.hidePayLayertwo();
    }
  },

})